package com.java.dao.Impl;

import com.java.dao.UserDao;
import com.java.entity.Users;
import com.java.util.C3P0Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
    @Override
    public Users selectUserByUnameAndByPwd(String uId, String pwd) throws SQLException {
        String sql="select * from f_users where userId=? and password=?";
        return qr.query(sql, new BeanHandler<>(Users.class), uId,pwd);
    }

    @Override
    public void insertUser(Users user) throws SQLException {
        //insert into f_users values('yan','123456','张三','男',18,'123456','134678946','张三界王三村');
        String sql="insert into f_users(userid,password,name,sex,age,id,phone,address) values(?,?,?,?,?,?,?,?)";
        Object[] objects={user.getUserid(),user.getPassword(),user.getName(),user.getSex(),user.getAge(),user.getId(),user.getPhone(),user.getAddress()};
        qr.update(sql,objects);
    }

    @Override
    public void updateUser(Users user) throws SQLException {
        String sql= "update f_users set userid=?, password=?,name=?,sex=?,age=?,phone=?,address=? where id=?";
        Object[] objects={user.getUserid(),user.getPassword(),user.getName(),user.getSex(),user.getAge(),user.getPhone(),user.getAddress(),user.getId()};
        qr.update(sql,objects);
    }
}
